package robot;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 11:13:59 2018/11/12
 * @version:2.0
 */
public class ColorUtil {
    public static BufferedImage getDesktop(int w, int h) {
        Robot rb = null; //在此用来抓取屏幕，即截屏。详细可见API
        try {
            rb = new Robot();
        } catch (AWTException e) {
            System.out.println("获取颜色异常");
            e.printStackTrace();
        }
        Rectangle rec = new Rectangle(0, 0, w, h);
        BufferedImage bi = rb.createScreenCapture(rec);
        return bi;
    }

    public static int getPixel(BufferedImage bi, int x, int y) {
        /*Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
        Dimension di = tk.getScreenSize(); //屏幕尺寸规格
        Rectangle rec = new Rectangle(0, 0, di.width, di.height);
        BufferedImage bi = rb.createScreenCapture(rec);*/
        int pixelColor = bi.getRGB(x, y);//获得自定坐标的像素值
        int[] rgb = new int[3];
        rgb[0] = (pixelColor & 0xff0000) >> 16;
        rgb[1] = (pixelColor & 0xff00) >> 8;
        rgb[2] = (pixelColor & 0xff);
        //System.out.print("| (" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ") ");
        //System.out.println(16777216 + pixelColor);
        // pixelColor的值为负，经过实践得出：加上颜色最大值就是实际颜色值。
        return 16777216 + pixelColor;
    }
}
